package ebusiness.ebusiness.Dao.TorlleyDao;

import ebusiness.ebusiness.Dao.DBManager;
import ebusiness.ebusiness.Dao.OrderDao.OrderQueryVO;
import ebusiness.ebusiness.Entity.Good;
import ebusiness.ebusiness.Entity.Order;
import ebusiness.ebusiness.Entity.Torlley;
import ebusiness.ebusiness.Entity.UserEntity.RegisteUser;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.Toy;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

public class TorlleyQueryVO {
public int addATemplory(Torlley torlley)throws Exception{
    return addATemplory(torlley.getCustomer_id(),torlley.getShopping_id(),
            torlley.getGood_id(),torlley.getGood_num(),torlley.getStatus(),torlley.getNun_order_form_id());

}

public int addATemplory(int customer_id,int shopping_id,int good_id,int good_num,int status,int nun_order_form_id)throws  Exception{
    SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
    try{
        TorlleyInter inter=sqlSession.getMapper(TorlleyInter.class);
        inter.addATemplory(customer_id, shopping_id, good_id, good_num, status, nun_order_form_id);
        sqlSession.commit();
        sqlSession.close();
        return 1;
    }catch (Exception e ){
        e.printStackTrace();
        sqlSession.close();
        return 0;
    }

}
public int modifyTemplory(Torlley torlley)throws Exception{
    return modifyTemplory(torlley.getCustomer_id(),torlley.getShopping_id(),torlley.getGood_id(),torlley.getGood_num()
    ,torlley.getStatus(),torlley.getNun_order_form_id());
}
public int modifyTemplory(int customer_id,int shopping_id,int good_id,int good_num,int status,int nun_order_form_id) throws  Exception{
    SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
    try {
        TorlleyInter inter = sqlSession.getMapper(TorlleyInter.class);
        inter.modifyTemplory(customer_id, shopping_id, good_id, good_num, status, nun_order_form_id);
        sqlSession.commit();
        sqlSession.close();
        return 1;
    }
    catch (Exception e) {
        sqlSession.close();
        e.printStackTrace();
        return 0;
    }

}
public int deleteTemplory(int id,int uid) throws Exception{
    SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
    try{
        TorlleyInter inter=sqlSession.getMapper(TorlleyInter.class);
        inter.deleteTemplory(id,uid);
        sqlSession.commit();
        sqlSession.close();
        return 1;
    }
    catch (Exception e){
        e.printStackTrace();
        sqlSession.close();
        return 0;
     }

}

public List<Torlley> getTemplories(int uid) throws Exception{
    SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
    try {
        TorlleyInter inter=sqlSession.getMapper(TorlleyInter.class);
        List<Torlley> torlleyList=inter.getTemplories(uid);
        sqlSession.close();
        sqlSession.close();
        return torlleyList;
    }catch (Exception e){
        sqlSession.close();
    }return null;

}
    public List<Integer> getAllOrderID() throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            TorlleyQueryVO orderInter=sqlSession.getMapper(TorlleyQueryVO.class);

            List<Integer> orderList= orderInter.getAllOrderID();
            sqlSession.close();
            return orderList;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }return null;


    };
    public   List<Integer> getAllDetailID() throws Exception{
        SqlSession sqlSession=DBManager.GetSqlFactory().openSession();
        try {
            TorlleyInter orderInter=sqlSession.getMapper(TorlleyInter.class);
            List<Integer> orderList= orderInter.getAllDetailID();
            sqlSession.close();
            return orderList;
        }catch (Exception e){
            sqlSession.close();
            e.printStackTrace();
        }return null;


    }
public static int BuildOrderID()throws Exception{
    int OrderID=(int)(Math.random()*1135423);

    SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
    TorlleyInter inter=sqlSession.getMapper(TorlleyInter.class);
    try {
        List<Integer> ids=inter.getAllOrderID();
        if(ids!=null&&ids.size()>0){
            for(int i=0;i<100;i++){
                if(ids.contains(OrderID))
                    OrderID=(int)(Math.random()*1135423);
                else {
                    sqlSession.close();
                    return OrderID;
                }
            }
            if(ids.contains(OrderID)){
                sqlSession.close();
                return -2;
            }

        }else {
            sqlSession.close();
            return OrderID;
        }

    }catch (Exception e){
        sqlSession.close();
        return -1;
    }
    sqlSession.close();
    return -3;
    }
    public static int BuildDetailOrderID()throws Exception{
        int OrderID=(int)(Math.random()*534551);

        SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
        TorlleyInter inter=sqlSession.getMapper(TorlleyInter.class);
        try {
            List<Integer> ids=inter.getAllDetailID();
            if(ids.size()>0){
                for(int i=0;i<100;i++){
                    if(ids.contains(OrderID))
                        OrderID=(int)(Math.random()*2735861);
                    else {
                        sqlSession.close();
                        return OrderID;
                    }

                }
                if(ids.contains(OrderID)){
                    sqlSession.close();
                    return -2;
                }

            }else {
                sqlSession.close();
                return OrderID;
            }

        }catch (Exception e){
e.printStackTrace();
sqlSession.close();
            return -1;
        }
        sqlSession.close();
        return -3;
    }

public void buildorder(List<Toy> gd,String create_time,String status,int uid,int payway,int addressid) throws Exception{
    SqlSession sqlSession = DBManager.GetSqlFactory().openSession();
    List<Integer> orders= new ArrayList<>();
    try{
        TorlleyInter inter=sqlSession.getMapper(TorlleyInter.class);



        for (Toy g:gd
             ) {
            int gid=g.getGood_id();
            if(g.getSelected()>0){
                int n=g.getNumber();
                double total=n*g.getGood_price();
                Date date=new Date();
                java.sql.Date dt=new java.sql.Date(date.getTime());
                	inter.buildorder(dt.toString(),dt.toString(),status,total,g.getBorrower_id_borrower_id(),g.getBrand_id_brand_id(),uid,payway,addressid);
                    
                    inter.deleteTemplory(g.getGood_id(),uid);


            }
        }
        sqlSession.commit();
        sqlSession.close();
        
    }
    catch (Exception e){
    	System.out.println(e);

        sqlSession.close();
        
    }
    finally {
        sqlSession.close();
    }
}
}
